from typing import Generator
from app.core.config import settings
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database
from sqlalchemy.orm import sessionmaker

engine = create_engine(settings.DATABASE_URL, pool_pre_ping=True)
if not database_exists(settings.DATABASE_URL):
    create_database(settings.DATABASE_URL)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)



def get_db() -> Generator:
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()
